package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.SRException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    DishMapper dishMapper;
    @Override
    @Transactional
    public void saveWihsFlavors(DishDto dishDto) {
        //首先添加菜品基本属性
        this.save(dishDto);

        //然后添加口味属性
        //拿到口味参数
        List<DishFlavor> flavors = dishDto.getFlavors();
        //现在口味属性id没有值需要设置口味id值 前提条件是菜品要先添加到数据库中
        //循环添加
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        //把拿到的口味集合批量添加到口味数据库
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> pageDishDto(Integer page, Integer pageSize, String name) {
        Page<DishDto> dtoPage = new Page<>(page,pageSize);
       Page<DishDto> dishes = dishMapper.pageDishDto(dtoPage,name);
       return dishes;
    }

    /**
     * 根据id查询回显
     * @param id
     * @return
     */
    @Override
    public DishDto getDishDtoById(Long id) {
       DishDto dishDtoById = dishMapper.getDishDtoById(id);
       return dishDtoById;
    }

    /**
     * 修改菜品逻辑
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateByDishDto(DishDto dishDto) {
        //菜品普通字段直接添加 根据id修属性值
        this.updateById(dishDto);
        /*口味数据相对复杂 用户会删除口味或添加口味此时变动过的口味的dishId是没有值的
        这时上面的菜品id是一直都有的,这时就需要有宁可杀错不可错过的心态,先把口味数据都删了然后
        在新增全部口味
         */
        //删除口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = Wrappers.lambdaQuery(DishFlavor.class)
                .eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //在新增口味
        //获取口味属性值集合
        List<DishFlavor> flavors = dishDto.getFlavors();
        //循环遍历存入dishId
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        //批量存入口味表
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteByIds(Long[] ids) {
        //正在售卖状态不可删除
        //delete from dish where status is not 1 and id in (...)
        Integer count = this.lambdaQuery().in(Dish::getId,Arrays.asList(ids)).eq(Dish::getStatus, 1).count();

        if(count > 0){
            throw new SRException("此商品正在售卖!");
        }

        //如果状态为0则删除
        this.removeByIds(Arrays.asList(ids));

        //菜品对应的口味也删
        dishFlavorService.update().in("dish_id",ids).remove();
    }

    /**
     * 分类查询对应菜品
     * @param categoryId
     * @param name
     * @return
     */
    @Override
    public List<DishDto> listDishDto(Long categoryId, String name) {
        /*//判断id和name
        LambdaQueryWrapper<Dish> queryWrapper = Wrappers.lambdaQuery(Dish.class)
                .eq(categoryId != null, Dish::getCategoryId, categoryId)
                .like(StringUtils.isNotBlank(name), Dish::getName, name)
                .eq(Dish::getStatus, 1);
        List<Dish> dishList = this.list(queryWrapper);*/

        return dishMapper.listDishDto(categoryId,name);
    }
}


